﻿using System;
using System.Web;

namespace OrganizationName.Factory.LifeTimeContracts
{
    public class HttpSessionLifeTimeContract : KeyedStorageLifeTimeContract
    {
        private System.Web.SessionState.HttpSessionState Session
        {
            get
            {
                return HttpContext.Current != null ? HttpContext.Current.Session : null;
            }
        }

        protected override object GetData(string slotName)
        {
            return Session != null ? Session[slotName] : null;
        }

        protected override void SetData(string slotName, object instance)
        {
            if (Session != null)
            {
                Session[slotName] = instance;
            }
        }

        public override object SyncRoot
        {
            get
            {
                return Session != null ? Session : new object();
            }
        }
    }
}
